﻿package mortal.game.view.Title
{
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.Title.data.util.*;
    import mortal.game.view.Title.renderer.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TitleModule extends Window
    {
        private var _bg:GBitmap;
        private var _titleHeadBg:GBitmap;
        private var _titleBodyBg:GBitmap;
        private var _tabBar:GTabarNew;
        private var _currTitleText:GTextFiled;
        private var _hideTitleBox:GCheckBox;
        private var _currAttrList:GTileList;
        private var _titleInfoList:GTileList;
        private var _pageSelecter:PageSelecter;
        private var _totalAttrList:GTileList;
        private var _ungetText:GTextFiled;
        private var _currSelTag:int;
        private var _rect3d:Rect3DObject;
        private var _scale9gridImg2d:Scale9GridImg2DEx;
        private var _currTitleEffectPlayer:EffectPlayer;
        private var _isCreateChildren:Boolean;
        private var _img2d:Img2D;
        private var _img2d_2:Img2D;
        private var _effectPlayer1:EffectPlayer;
        private var _effectPlayer2:EffectPlayer;
        private var _effectPlayer3:EffectPlayer;
        private var _effectPlayer4:EffectPlayer;

        public function TitleModule(param1:ILayer = null)
        {
            super(param1);
            this.setSize(603, 533);
            this.mouseEnabled = false;
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gBitmap("", 4 - 8, -32, this);
            this.addChildAt(this._bg, 0);
            this.init3DRect();
            this._titleHeadBg = UIFactory.gBitmap("", 0, 0, null);
            this._titleBodyBg = UIFactory.gBitmap("", 0, 0, null);
            LoaderManager.instance.load(ImagesConst.ExtendWindowBG2 + ".swf", this.onBgLoadCompl);
            LoaderManager.instance.load(ImagesConst.TitleBodyBg + ".swf", this.onTitleHeadBgLoadCompl);
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            _closeBtn.x = this.width - _closeBtn.width - 5 - 11;
            _closeBtn.y = 5 + 9;
            return;
        }// end function

        private function onBgLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bg.bitmapData = param1.bitmapData as BitmapData;
            return;
        }// end function

        private function onTitleHeadBgLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._titleBodyBg.bitmapData = param1.bitmapData as BitmapData;
            this.add3DBg();
            LoaderHelp.addResCallBack(ResFileConst.title, this.onResLoadCompl);
            return;
        }// end function

        private function onTitleBodyBgLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._titleBodyBg.bitmapData = param1.bitmapData as BitmapData;
            this.add3DBg();
            LoaderHelp.addResCallBack(ResFileConst.title, this.onResLoadCompl);
            return;
        }// end function

        private function onResLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            this._isCreateChildren = true;
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Title_text, 254 - 4, 8 - 5, this));
            this._tabBar = UIFactory.gTabBarNew(2, 91 - 4 - 49, Language.getArray(40205), 603 - 8, 533, 98, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41389), 56 + 3 - 12, 25 + 59 - 14, 80, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatHuang));
            var _loc_1:* = GlobalStyle.textFormatHuang2;
            _loc_1.size = 13;
            _loc_1.align = AlignMode.CENTER;
            this._currTitleText = UIFactory.gTextField("", 0, 50 + 59 - 14, 150, 25, this.contentTopOf3DSprite, _loc_1);
            this._hideTitleBox = UIFactory.checkBox(Language.getString(41390), 19, 75 + 59 - 14, 103, 25, this.contentTopOf3DSprite);
            this._hideTitleBox.configEventListener(MouseEvent.CLICK, this.onHideTitle);
            var _loc_2:* = GlobalStyle.textFormatHuang;
            _loc_2.size = 16;
            this._ungetText = UIFactory.gTextField(Language.getString(41391), 208 + 15, 100, 230, 35, this.contentTopOf3DSprite, _loc_2);
            this._ungetText.visible = false;
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41392), 56 + 3 - 12 + 416, 25 + 59 - 14, 80, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatHuang));
            this._currAttrList = UIFactory.tileList(56 + 3 - 12 + 401, 30 + 58, 130, 80, this.contentTopOf3DSprite);
            this._currAttrList.columnWidth = 128;
            this._currAttrList.rowHeight = 19;
            this._currAttrList.horizontalGap = 0;
            this._currAttrList.verticalGap = 0;
            this._currAttrList.setStyle("skin", new Bitmap());
            this._currAttrList.setStyle("cellRenderer", TitleAttrListCellRenderer);
            this._titleInfoList = UIFactory.tileList(8 + 5 - 2, 144 + 59 - 24 - 2 - 7, 580, 231, this.contentTopOf3DSprite);
            this._titleInfoList.columnWidth = 284 + 4;
            this._titleInfoList.rowHeight = 104 + 4;
            this._titleInfoList.horizontalGap = 5 - 3;
            this._titleInfoList.verticalGap = 5 - 3;
            this._titleInfoList.setStyle("skin", new Bitmap());
            this._titleInfoList.setStyle("cellRenderer", TitleInfoCellRenderer);
            var _loc_3:* = GlobalStyle.textFormatBai;
            _loc_3.align = AlignMode.CENTER;
            this._pageSelecter = UIFactory.pageSelecter(225 + 4, 398 + 25 - 24 - 8, this.contentTopOf3DSprite, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.InputBg, _loc_3);
            this._pageSelecter.pageTextBoxSize = 50;
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41393), 416 + 15, 400 + 22 - 24 - 8, 170, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatLv));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41394), 190 - 3, 400 + 22 + 27 - 24 - 6, 250, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai));
            this._totalAttrList = UIFactory.tileList(43 - 2, 452 + 23 - 24 - 8, 564, 58, this.contentTopOf3DSprite);
            this._totalAttrList.columnWidth = 141;
            this._totalAttrList.rowHeight = 19;
            this._totalAttrList.horizontalGap = 0;
            this._totalAttrList.verticalGap = 0;
            this._totalAttrList.setStyle("skin", new Bitmap());
            this._totalAttrList.setStyle("cellRenderer", TitleAttrListCellRenderer);
            this.initView();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.TitleEquipTitle, this.equipUnloadTitleHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.TitleEquipTitle, this.equipUnloadTitleHandler);
            return;
        }// end function

        private function initView() : void
        {
            this.updateCurrTitleInfo();
            this.initHideState();
            this.updateTotalAttrList();
            this.tabBarChangeHandler();
            return;
        }// end function

        private function updateCurrTitleInfo() : void
        {
            var _loc_3:String = null;
            var _loc_1:* = Cache.instance.title.currTitle;
            if (_loc_1)
            {
                this._currTitleText.text = _loc_1.titleData.name;
                this._ungetText.visible = false;
            }
            else
            {
                this._currTitleText.text = "";
                this._ungetText.visible = true;
                if (Cache.instance.title.ownTitles.length != 0)
                {
                    this._ungetText.text = Language.getString(41395);
                }
                else
                {
                    this._ungetText.text = Language.getString(41396);
                }
            }
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._currTitleEffectPlayer);
                if (_loc_1)
                {
                    _loc_3 = _loc_1.titleData.icon;
                    this._currTitleEffectPlayer = EffectPlayerPool.instance.getEffectPlayer(_loc_3, null, this._rect3d.renderList, false);
                    this._currTitleEffectPlayer.play();
                    this._rect3d.addObject3d(this._currTitleEffectPlayer, 50 + 240, 90 - 20 - 24);
                }
            }
            var _loc_2:* = new DataProvider();
            if (_loc_1)
            {
                _loc_2.addItems(TitleUtil.getTitleAttrData2(_loc_1.titleData));
            }
            this._currAttrList.dataProvider = _loc_2;
            this._currAttrList.drawNow();
            return;
        }// end function

        private function clearCurrTitleInfo() : void
        {
            this._currTitleText.text = "";
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._currTitleEffectPlayer);
                this._currTitleEffectPlayer = null;
            }
            var _loc_1:* = new DataProvider();
            this._currAttrList.dataProvider = _loc_1;
            this._currAttrList.drawNow();
            return;
        }// end function

        private function updateNameOnTurnPage(param1:Array) : void
        {
            var _loc_2:* = new DataProvider();
            _loc_2.addItems(param1);
            this._titleInfoList.dataProvider = _loc_2;
            this._titleInfoList.drawNow();
            this._titleInfoList.selectedIndex = -1;
            return;
        }// end function

        private function updateTotalAttrList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = TitleUtil.getTotalGainAttrData2();
            _loc_1.addItems(_loc_2);
            this._totalAttrList.dataProvider = _loc_1;
            this._totalAttrList.drawNow();
            return;
        }// end function

        private function initHideState() : void
        {
            if (ClientSetting.local.getIsDone(IsDoneType.HideTitle))
            {
                this._hideTitleBox.selected = true;
            }
            else
            {
                this._hideTitleBox.selected = false;
            }
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._currSelTag = this._tabBar.selectedIndex + 1;
            var _loc_2:* = Cache.instance.title.getDataByType(this._currSelTag);
            this.updatePageInfo();
            this.updateCurrPageInfo();
            return;
        }// end function

        private function updatePageInfo() : void
        {
            var _loc_1:* = Cache.instance.title.getDataByType(this._currSelTag);
            this._pageSelecter.maxPage = Math.ceil(_loc_1.length / TitleConst.PageSize);
            return;
        }// end function

        private function updateCurrPageInfo() : void
        {
            var _loc_1:* = Cache.instance.title.getDataByType(this._currSelTag);
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * TitleConst.PageSize;
            var _loc_3:* = this._pageSelecter.currentPage * TitleConst.PageSize;
            var _loc_4:* = _loc_1.slice(_loc_2, _loc_3);
            this.updateNameOnTurnPage(_loc_4);
            this.updateEffectOnTurnPage(_loc_4);
            return;
        }// end function

        private function init3DRect() : void
        {
            if (this._rect3d == null)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(8 - 2, 32 + 33, 590, 445), this);
                Rect3DManager.instance.windowShowHander(null, this);
            }
            if (this._scale9gridImg2d == null)
            {
            }
            return;
        }// end function

        private function add3DBg() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d_2);
                this._img2d_2 = new Img2D(null, this._titleBodyBg.bitmapData, new Rectangle(0, 0, 589, 445), true);
                this._rect3d.addImg(this._img2d_2);
            }
            return;
        }// end function

        private function onHeadLoadCompl() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d);
                this._img2d_2 = new Img2D(null, this._titleHeadBg.bitmapData, new Rectangle(0, 0, 589, 105), true);
                this._rect3d.addImg(this._img2d);
            }
            return;
        }// end function

        private function onBodyLoadCompl() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d_2);
                this._img2d_2 = new Img2D(null, this._titleBodyBg.bitmapData, new Rectangle(0, 0, 589, 341), true);
                this._img2d_2.y = 109 + 25 - 24;
                this._rect3d.addImg(this._img2d_2);
            }
            return;
        }// end function

        private function updateEffectOnTurnPage(param1:Array) : void
        {
            this.removeAllEffect();
            var _loc_2:* = param1[0] as TitleDataInfo;
            if (_loc_2)
            {
                this._effectPlayer1 = EffectPlayerPool.instance.getEffectPlayer(_loc_2.titleData.icon, null, this._rect3d.renderList, false);
                this._effectPlayer1.play();
                this._rect3d.addObject3d(this._effectPlayer1, 72 + 75 - 5 + 6, 154 + 45 - 24);
            }
            _loc_2 = param1[1] as TitleDataInfo;
            if (_loc_2)
            {
                this._effectPlayer2 = EffectPlayerPool.instance.getEffectPlayer(_loc_2.titleData.icon, null, this._rect3d.renderList, false);
                this._effectPlayer2.play();
                this._rect3d.addObject3d(this._effectPlayer2, 354 + 78 + 6, 154 + 45 - 24);
            }
            _loc_2 = param1[2] as TitleDataInfo;
            if (_loc_2)
            {
                this._effectPlayer3 = EffectPlayerPool.instance.getEffectPlayer(_loc_2.titleData.icon, null, this._rect3d.renderList, false);
                this._effectPlayer3.play();
                this._rect3d.addObject3d(this._effectPlayer3, 72 + 75 - 5 + 6, 252 + 45 + 10 - 24);
            }
            _loc_2 = param1[3] as TitleDataInfo;
            if (_loc_2)
            {
                this._effectPlayer4 = EffectPlayerPool.instance.getEffectPlayer(_loc_2.titleData.icon, null, this._rect3d.renderList, false);
                this._effectPlayer4.play();
                this._rect3d.addObject3d(this._effectPlayer4, 354 + 78 + 6, 252 + 45 + 10 - 24);
            }
            return;
        }// end function

        private function removeAllEffect() : void
        {
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._effectPlayer1);
                this._rect3d.disposeObj3d(this._effectPlayer2);
                this._rect3d.disposeObj3d(this._effectPlayer3);
                this._rect3d.disposeObj3d(this._effectPlayer4);
                this._effectPlayer1 = null;
                this._effectPlayer2 = null;
                this._effectPlayer3 = null;
                this._effectPlayer4 = null;
            }
            return;
        }// end function

        private function remove3D() : void
        {
            if (this._rect3d)
            {
                if (this._currTitleEffectPlayer)
                {
                    this._rect3d.disposeObj3d(this._currTitleEffectPlayer);
                    this._currTitleEffectPlayer = null;
                }
                if (this._img2d)
                {
                    this._img2d.dispose();
                    this._img2d = null;
                }
                if (this._img2d_2)
                {
                    this._img2d_2.dispose();
                    this._img2d_2 = null;
                }
                if (this._scale9gridImg2d)
                {
                    this._rect3d.disposeObj3d(this._scale9gridImg2d);
                    this._scale9gridImg2d = null;
                }
                this.removeAllEffect();
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            return;
        }// end function

        private function onHideTitle(event:MouseEvent) : void
        {
            if (this._hideTitleBox.selected)
            {
                ClientSetting.local.setIsDone(true, IsDoneType.HideTitle);
                GameProxy.titleProxy.hideTitle(0);
            }
            else
            {
                ClientSetting.local.setIsDone(false, IsDoneType.HideTitle);
                GameProxy.titleProxy.hideTitle(1);
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateCurrPageInfo();
            return;
        }// end function

        private function equipUnloadTitleHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as TitleDataInfo;
            if (_loc_2.titleStatus == ETitleState._ETitleStateNotGet)
            {
                MsgManager.showRollTipsMsg(Language.getString(41397));
                return;
            }
            var _loc_3:* = Cache.instance.title.currTitle;
            if (_loc_3 && _loc_2.titleData.titleId == _loc_3.titleData.titleId)
            {
                GameProxy.titleProxy.unLoadTitle();
                Cache.instance.title.clearCurrTitle();
                this.clearCurrTitleInfo();
            }
            else
            {
                GameProxy.titleProxy.enableTitle(_loc_2.titleData.titleId);
                this._hideTitleBox.selected = false;
            }
            return;
        }// end function

        public function resetHideStatus() : void
        {
            this._hideTitleBox.selected = false;
            return;
        }// end function

        public function updateAll() : void
        {
            if (this._isCreateChildren)
            {
                this.updateCurrTitleInfo();
                this.updatePageInfo();
                this.updateCurrPageInfo();
                this.updateTotalAttrList();
            }
            return;
        }// end function

        public function onTitleUpdate(param1:TitleDataInfo, param2:int) : void
        {
            this.updateAll();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._isCreateChildren)
            {
                this._isCreateChildren = false;
                this._tabBar.dispose();
                this._currTitleText.dispose();
                this._hideTitleBox.dispose();
                this._currAttrList.dispose();
                this._titleInfoList.dispose();
                this._pageSelecter.dispose();
                this._totalAttrList.dispose();
                this._ungetText.dispose();
                this._tabBar = null;
                this._currTitleText = null;
                this._hideTitleBox = null;
                this._currAttrList = null;
                this._titleInfoList = null;
                this._pageSelecter = null;
                this._totalAttrList = null;
                this._ungetText = null;
                this.removeListeners();
            }
            this._bg.dispose();
            this._titleHeadBg.dispose();
            this._titleBodyBg.dispose();
            this._bg = null;
            this._titleHeadBg = null;
            this._titleBodyBg = null;
            this._currSelTag = 0;
            this.remove3D();
            return;
        }// end function

    }
}
